import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    name: 'Login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: "/home/user/index",
    component: () => import('../views/home/index.vue'),
    children: [
      // {
      //   path: '/home/index',
      //   name: 'home_index',
      //   component: () => import('../views/home/home/index.vue')
      // },
      {
        path: '/home/user/index',
        name: 'home_user',
        component: () => import('../views/home/home/user/index.vue')
      },
      {
        path: '/home/gallery/index',
        name: 'home_gallery',
        component: () => import('../views/home/home/gallery/index.vue')
      },
      {
        path: '/home/gallery/type',
        name: 'home_gallery_type',
        component: () => import('../views/home/home/gallery/type.vue')
      },
      {
        path: '/home/meaty/index',
        name: 'home_meaty',
        component: () => import('../views/home/home/meaty/index.vue')
      },
      {
        path: '/home/meaty/type',
        name: 'home_meaty_type',
        component: () => import('../views/home/home/meaty/type.vue')
      },
      {
        path: '/home/curing/index',
        name: 'home_curing',
        component: () => import('../views/home/home/curing/index.vue')
      },
      {
        path: '/home/information/index',
        name: 'home_information',
        component: () => import('../views/home/home/information/index.vue')
      },
      {
        path: '/home/soil/index',
        name: 'home_soil',
        component: () => import('../views/home/home/soil/index.vue')
      },
      
      {
        path: '/home/system/img',
        name: 'home_system_img',
        component: () => import('../views/home/home/system/img.vue')
      },
      {
        path: '/home/system/tail',
        name: 'home_system_tail',
        component: () => import('../views/home/home/system/tail.vue')
      },
      {
        path: '/home/personal/index',
        name: 'home_personal',
        component: () => import('../views/home/home/personal/index.vue')
      },
    ]
  },
]

const router = new VueRouter({
  routes,
  // base: 'bff', // nginx配置
  mode: 'history', // 去掉url中的#
})
router.beforeEach((to, from, next) => {
  // console.log(to);

  let isLogin = JSON.parse(localStorage.getItem("admin"))
  // //我在这里模仿了一个获取用户信息的方法
  // let isLogin = window.sessionStorage.getItem('userInfo');
  if (isLogin) {
    //如果用户信息存在则往下执行。
    if (to.path === '/login' ||to.path === '/') {
      next('/home')
    } else {
      next()
    }
    
  } else {
    //如果用户token不存在则跳转到login页面
    // console.log(to);

    if (to.path === '/login' ) {
      next()
    } else {
      next('/login')
    }
  }
  next()
})
export default router
